<?php

namespace App\Http\Services;


use App\Http\Model\GiftBagIntimacyLog;
use Illuminate\Support\Facades\Redis;

class GiftBagIntimacyServices extends CommonServices
{

    const REDIS_INTIMACY_NAME = 'INTIMACYVALUE:';//亲密值
    const REDIS_SEND_RANK_NAME = 'SENDRANKLIST:'; //送礼榜
    const REDIS_COLLECT_RANK_NAME = 'COLLECTRANKLIST:';//收礼榜

    public function __construct()
    {
        //
        $this->giftBagIntimacyLog = new GiftBagIntimacyLog();
        $this->config = new ConfigServices();
    }

    public function checkCpUser($to_user = [])
    {
        $res = $this->giftBagIntimacyLog->getCpUser($to_user);
        if (!$res) {
            return false;
        }
        return $res;

    }

    /*
     * @param [$send_user]   送用户
     * @param [$to_user]   收取用户
     * */
    public function createIntimacyLog($cp, $num)
    {
        return $this->giftBagIntimacyLog->create($cp, $num);
    }

    public function getRankList($send_user, $to_user, $num)
    {
        return $this->giftBagIntimacyLog->rankList($send_user, $to_user, $num);

    }


    /*
     * @param [$user_id]   用户
     * */
    public function getIntimacy($user_id)
    {
        return $this->giftBagIntimacyLog->userIntimacy($user_id);
    }


    /*
     * @param [$user_id]   用户
     * @param [$type] 1送礼 2收礼
     * */
    public function getGiftRank($user_id)
    {
        $res = $this->giftBagIntimacyLog->giftRank();
        $send_rank = array_flip($res['send_rank']);
        $collect_rank = array_flip($res['collect_rank']);

        if (!isset($send_rank[$user_id])) {
            $send_rank[$user_id] = '未上榜';
        } else {
            $send_rank[$user_id]++;
        }
        if (!isset($collect_rank[$user_id])) {
            $collect_rank[$user_id] = '未上榜';
        } else {
            $collect_rank[$user_id]++;
        }
        $data['send_rank'] = $send_rank[$user_id];
        $data['collect_rank'] = $collect_rank[$user_id];
        return $data;
    }

    public function getRank()
    {
        return $this->giftBagIntimacyLog->giftRank();
    }


    public function awardGift()
    {
        $res = $this->giftBagIntimacyLog->giftRank();
        do {
            if (empty($res['send_rank'])) {
                return false;
                break;
            }
            if (empty($res['collect_rank'])) {
                return false;
                break;
            }
            $this->giftAwardToUserId($res['send_rank'], 1);
            $this->giftAwardToUserId($res['collect_rank'], 2);
            break;
        } while (false);
        return true;
    }

    public function anewAwardGift()
    {
        $res = $this->giftBagIntimacyLog->giftRank();
        do {
            if (empty($res['send_rank'])) {
                return false;
                break;
            }
            if (empty($res['collect_rank'])) {
                return false;
                break;
            }
            $this->anewGiftAwardToUserId($res['send_rank'], 1);
            $this->anewGiftAwardToUserId($res['collect_rank'], 2);
            break;
        } while (false);
        return true;
    }

    //发放奖励   1：头饰 2：座驾  3：气泡
    public function anewGiftAwardToUserId($user_ids, $type)
    {
        $config = $this->config->getConfig(3);
        $gift_bag_bubble_id = explode(',', $config['gift_bag_bubble_id']['value']);
        $gift_bag_headwear_ids = explode(',', $config['gift_bag_headwear_ids']['value']);
        foreach ($user_ids as $k => $val) {
            $v['user_id'] = $val;
            $v['type'] = $type;
            if($k > 4){
                $this->giftBagIntimacyLog->giveUserApi($v, [
                    'goods_id' => $gift_bag_headwear_ids[5],
                    'goods_type' => 1,
                    'time_limit' => 7,
                ]);
                $this->giftBagIntimacyLog->giveUserApi($v, [
                    'goods_id' => $gift_bag_bubble_id[5],
                    'goods_type' => 3,
                    'time_limit' => 7,
                ]);
                $this->giftBagIntimacyLog->sendMsgApi($v, '这个暑假恋爱吧——"壁咚"奖励已发放，您可到个性装扮查收礼品哦~');
            }
        }
        return true;
    }


    //发放奖励   1：头饰 2：座驾  3：气泡
    public function giftAwardToUserId($user_ids, $type)
    {
        $config = $this->config->getConfig(3);
        $gift_bag_bubble_id = explode(',', $config['gift_bag_bubble_id']['value']);
        $gift_bag_headwear_ids = explode(',', $config['gift_bag_headwear_ids']['value']);
        $gift_bag_mount_ids = explode(',', $config['gift_bag_mount_ids']['value']);
        foreach ($user_ids as $k => $val) {
            $v['user_id'] = $val;
            $v['type'] = $type;
            switch ($k) {
                case 0;
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[$k],
                        'goods_type' => 1,
                        'time_limit' => 30,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_mount_ids[$k],
                        'goods_type' => 2,
                        'time_limit' => 15,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[$k],
                        'goods_type' => 3,
                        'time_limit' => 30,
                    ]);
                    break;
                case 1;
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[$k],
                        'goods_type' => 1,
                        'time_limit' => 15,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_mount_ids[$k],
                        'goods_type' => 2,
                        'time_limit' => 7,
                    ]);
                    //修改
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[$k],
                        'goods_type' => 3,
                        'time_limit' => 30,
                    ]);
                    break;
                case 2;
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[$k],
                        'goods_type' => 1,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_mount_ids[$k],
                        'goods_type' => 2,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[$k],
                        'goods_type' => 3,
                        'time_limit' => 7,
                    ]);
                    break;
                case 3;
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[$k],
                        'goods_type' => 1,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_mount_ids[$k],
                        'goods_type' => 2,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[$k],
                        'goods_type' => 3,
                        'time_limit' => 30,
                    ]);
                    $this->giftBagIntimacyLog->awardPearlApi($v, 1000);
                    break;
                case 4;
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[$k],
                        'goods_type' => 1,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_mount_ids[$k],
                        'goods_type' => 2,
                        'time_limit' => 15,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[$k],
                        'goods_type' => 3,
                        'time_limit' => 30,
                    ]);
                    $this->giftBagIntimacyLog->awardPearlApi($v, 500);
                    break;
                default:
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_headwear_ids[5],
                        'goods_type' => 1,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->giveUserApi($v, [
                        'goods_id' => $gift_bag_bubble_id[5],
                        'goods_type' => 3,
                        'time_limit' => 7,
                    ]);
                    $this->giftBagIntimacyLog->awardPearlApi($v, 200);
            }
            $this->giftBagIntimacyLog->sendMsgApi($v, '这个暑假恋爱吧——"壁咚"奖励已发放，您可到个性装扮查收礼品哦~');
        }
        return true;
    }


    public function checkValue($time)
    {
        $this->giftBagIntimacyLog->checkVaule($time);
        $this->giftBagIntimacyLog->updateIntimacy($time);
        return true;
    }

    public function logCreate($time)
    {
        $this->createIntimacyTableLog($time);
        $this->createScoreTableLog($time);
        return true;
    }


    //亲密值
    public function createIntimacyTableLog($time)
    {
        $data = $this->giftBagIntimacyLog->timeToUserGift($time, 'gift_pearl_stream', 35); //小屋礼物补差
        $user_id = array_unique(array_column($data, 'user_id'));//收用户
        $checkCp = $this->giftBagIntimacyLog->getCpUser($user_id);
        if (!$checkCp) {
            return false;
        }
        $user_value = [];//用户收数量  & 总亲密值
        foreach ($data as $v) {
            foreach ($checkCp as $val) {
                if (empty($user_value[$val['user_id']]['gift_num'])) $user_value[$val['user_id']]['gift_num'] = 0;
                if (empty($user_value[$val['user_id']]['intimacy_value'])) $user_value[$val['user_id']]['intimacy_value'] = 0;
                if (empty($user_value[$val['user_id']]['cp_user_id'])) $user_value[$val['user_id']]['cp_user_id'] = '';
                if ($v['user_id'] == $val['user_id']) {
                    $user_value[$val['user_id']]['gift_num'] += $v['gift_num'];
                    $user_value[$val['user_id']]['cp_user_id'] = $val['cp_user_id'];
                    $user_value[$val['user_id']]['intimacy_value']++;
                }
            }
        }
        foreach ($user_value as $k => $v) {
            //获取用户亲密值 & 积分
            $user_id = $k; // 收礼用户
            $old_num = Redis::hget(self::REDIS_INTIMACY_NAME . $k, 'value') ?? 0;
            if (empty($old_num)) {
                write_log('newIntimacy', 'newIntimacyTableError', $k);//用户亲密值log失败
                continue;
            }

            $new_intimacy = $v['gift_num'] - $v['intimacy_value'];
            if ($v['gift_num'] > 1000) {
                $v['gift_num'] = 1000;
                $new_intimacy = 0;
            }
            $new_num = $v['gift_num'] * 5;
            $type = 3;
            $this->giftBagIntimacyLog->creaeteLog($user_id, $type, $old_num, $new_num); // 应得积分
            $old_num = $old_num / 5;
            $new_num = $new_intimacy;
            $type = 4;
            $this->giftBagIntimacyLog->creaeteLog($user_id, $type, $old_num, $new_num); //亲密值补发
        }


    }


    //积分
    public function createScoreTableLog($time)
    {
        $send_name = self::REDIS_SEND_RANK_NAME; // 送
        $collect_name = self::REDIS_COLLECT_RANK_NAME;//收
        $data = $this->giftBagIntimacyLog->timeToUserGift($time, 'gift_stream', 89); //壁咚礼物补差
        $from_user = array_unique(array_column($data, 'from_user_id'));//送用户
        $user_id = array_unique(array_column($data, 'user_id'));//收用户
        $from_user_value = [];//用户送礼数量
        $user_value = [];//用户收礼数量
        foreach ($data as $v) {
            foreach ($from_user as $vv) {
                if (empty($from_user_value[$vv]['gift_num'])) $from_user_value[$vv]['gift_num'] = 0;
                if (empty($from_user_value[$vv]['rank_num'])) $from_user_value[$vv]['rank_num'] = 0;
                if ($v['from_user_id'] == $vv) {
                    $from_user_value[$vv]['gift_num'] += $v['gift_num'];
                    $from_user_value[$vv]['rank_num']++;//已送名次积分
                }
            }
            foreach ($user_id as $val) {
                if (empty($user_value[$val]['gift_num'])) $user_value[$val]['gift_num'] = 0;
                if (empty($user_value[$val]['rank_num'])) $user_value[$val]['rank_num'] = 0;
                if ($v['user_id'] == $val) {
                    $user_value[$val]['gift_num'] += $v['gift_num'];
                    $user_value[$val]['rank_num']++;
                }
            }
        }
        //送
        foreach ($from_user_value as $k => $v) {
            $send_num = Redis::hget($send_name . $k, 'rank');
            if (!$send_num) {
                write_log('newIntimacy', 'newScoreSendTableError', $k);//用户补发记录表生成失败
                continue; //用户送礼榜为空
            }
            $user_id = $k; // 送用户
            $old_num = $send_num;
            $new_num = $v['gift_num'];
            $type = 1;
            $this->giftBagIntimacyLog->creaeteLog($user_id, $type, $old_num, $new_num); // 送礼补发
        }
        //收
        foreach ($user_value as $k => $v) {
            $collect_num = Redis::hget($collect_name . $k, 'rank');
            if (!$collect_num) {
                write_log('newIntimacy', 'newScoreCollectTableError', $k);//用户补发记录表生成失败
                continue; //用户送礼榜为空
            }
            $user_id = $k; // 收礼用户
            $old_num = $collect_num;
            $new_num = $v['gift_num'];
            $type = 2;
            $this->giftBagIntimacyLog->creaeteLog($user_id, $type, $old_num, $new_num); // 送礼补发
        }
        return true;
    }

}
